package com.example.attrimirror.ui.attribute;

import android.app.Application;
import android.os.Environment;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.example.attrimirror.model.Attribute;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class AttributeViewModel extends AndroidViewModel {
    private final MutableLiveData<List<Attribute>> attributes;
    private final Gson gson;

    public AttributeViewModel(Application application) {
        super(application);
        attributes = new MutableLiveData<>(new ArrayList<>());
        gson = new Gson();
        loadAttributes();
    }

    private void loadAttributes() {
        File dataDir = new File(Environment.getExternalStorageDirectory(), "AttriMirror/data");
        File attributeFile = new File(dataDir, "attribute.json");
        
        try (FileReader reader = new FileReader(attributeFile)) {
            Type type = new TypeToken<List<Attribute>>(){}.getType();
            List<Attribute> attributeList = gson.fromJson(reader, type);
            attributes.setValue(attributeList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public LiveData<List<Attribute>> getAttributes() {
        return attributes;
    }
} 